package com.sharemarking.wa.platform.service;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.SysHttpStatus;
import com.sharemarking.wa.common.exception.RequestException;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by king on 2018/06/12.
 */
@Service
public class SmsService {

    //初始化ascClient需要的几个参数
    private final String product = "Dysmsapi";
    private final String domain = "dysmsapi.aliyuncs.com";

    //@Value("${ali.secret.accessKeyId}")
    private String accessKeyId = "LTAIMYb9kOLAcSo3";
    //@Value("${ali.secret.accessKeySecret}")
    private String accessKeySecret = "GDvwfBCk7z8v0xzx9UKgrgyYvQLD3j";
    //@Value("${ali.sms.code}")
    private String smsCode = "SMS_171191284";

    public boolean sendValidateCode(String mobile,String code){
    	try {
    		IClientProfile profile = DefaultProfile.getProfile("cn-shenzhen", accessKeyId,
                    accessKeySecret);
            DefaultProfile.addEndpoint("cn-shenzhen", "cn-shenzhen", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            request.setMethod(MethodType.POST);
            request.setPhoneNumbers(mobile);
            request.setSignName("中国创新创业成果交易会");
            request.setTemplateCode(smsCode);
            request.setTemplateParam("{\"code\":\""+code+"\"}");
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            System.out.println(sendSmsResponse.getCode());
            if(sendSmsResponse.getCode() != null ){
                switch (sendSmsResponse.getCode()){
                    //业务限流(每小时发送5次)
                    case "isv.BUSINESS_LIMIT_CONTROL":
                        throw RequestException.create(ResponseParams.error(SysHttpStatus.VALIDATE_CODE_MAX));
                    //发送成功
                    case "OK": return true;
                    //其他异常
                    default: return false;
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return false;
    }


    public boolean sendNotifyMessage(String mobile, String code, Map<String,String> params) throws Exception {
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,
                accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(mobile);
        request.setSignName("爱奇会展");
        request.setTemplateCode(code);
        String jsonStr = "";
        //遍历参数
        for(String key : params.keySet()){
            String value = params.get(key);
            System.out.println(key+"-"+value);
            jsonStr = jsonStr+"\""+key+"\":"+"\""+value+"\",";
        }
        request.setTemplateParam("{"+jsonStr.substring(0,jsonStr.length()-1)+"}");
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        System.out.println(sendSmsResponse.getCode());
        if(sendSmsResponse.getCode() != null ){
            switch (sendSmsResponse.getCode()){
                //业务限流(每小时发送5次)
                case "isv.BUSINESS_LIMIT_CONTROL":
                    throw RequestException.create(ResponseParams.error(SysHttpStatus.VALIDATE_CODE_MAX));
                    //发送成功
                case "OK": return true;
                //其他异常
                default: return false;
            }
        }else{
            throw RequestException.create(ResponseParams.error(SysHttpStatus.ERROR));
        }
    }

}
